home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12035 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: rcp6.elan.af.mil!rscernix!danpop
  2. Newsgroups: comp.lang.c
  3. Subject: Re: sprintf() question
  4. Message-ID: <danpop.827968757@rscernix>
  5. From: danpop@mail.cern.ch (Dan Pop)
  6. Date: 27 Mar 96 23:19:17 GMT
  7. References: <31593522.76B3@cbm.com>
  8. Organization: CERN European Lab for Particle Physics
  9. NNTP-Posting-Host: ues5.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11.  
  12. In <31593522.76B3@cbm.com> Dave Payne <paynedc@cbm.com> writes:
  13.  
  14. >Question on sprintf():
  15. >
  16. >Is it safe, and more importantly, is it ANSI standard, to use sprintf()
  17. >to print into the same variable?  Consider this example:
  18. >
  19. >char foo[100] = "foobar";
  20. >
  21. >sprintf(foo,"%s%s","the string is",foo);
  22. >
  23. >--------------------------------------------
  24. >
  25. >Would the resulting string be "the string is foobar", or will this
  26. >code cause problems because I'm using the same variable (foo) to
  27. >print to as well as read from?
  28.  
  29.     4.9.6.5 The sprintf function
  30.  
  31.     Synopsis
  32.  
  33.          #include <stdio.h>
  34.          int sprintf(char *s, const char *format, ...);
  35.  
  36.     Description
  37.  
  38.     The sprintf function is equivalent to fprintf , except that the
  39.     argument s specifies an array into which the generated output is to be
  40.     written, rather than to a stream.  A null character is written at the
  41.     end of the characters written; it is not counted as part of the
  42.     returned sum.  If copying takes place between objects that overlap,
  43.     the behavior is undefined.
  44.  
  45. The last phrase answers your question.
  46.  
  47. Dan
  48. --
  49. Dan Pop
  50. CERN, CN Division
  51. Email: danpop@mail.cern.ch 
  52. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  53.